package com.ruoyi.web.sso.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.sso.common.util.AssertUtil;
import com.ruoyi.web.sso.common.util.UuidUtil;
import com.ruoyi.web.sso.domain.GroupUser;
import com.ruoyi.web.sso.mapper.GroupUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.sso.mapper.GroupMapper;
import com.ruoyi.web.sso.domain.Group;
import com.ruoyi.web.sso.service.IGroupService;
import com.ruoyi.common.core.text.Convert;

/**
 * 集团信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-28
 */
@Service
public class GroupServiceImpl implements IGroupService 
{
    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private  GroupUserMapper groupUserMapper;

    /**
     * 查询集团信息
     * 
     * @param id 集团信息主键
     * @return 集团信息
     */
    @Override
    public Group selectGroupById(String id)
    {
        return groupMapper.selectGroupById(id);
    }

    /**
     * 查询集团信息列表
     * 
     * @param group 集团信息
     * @return 集团信息
     */
    @Override
    public List<Group> selectGroupList(Group group)
    {
        return groupMapper.selectGroupList(group);
    }

    /**
     * 新增集团信息
     * 
     * @param group 集团信息
     * @return 结果
     */
    @Override
    public int insertGroup(Group group)
    {
        group.setCreateTime(DateUtils.getNowDate());
        return groupMapper.insertGroup(group);
    }

    /**
     * 修改集团信息
     * 
     * @param group 集团信息
     * @return 结果
     */
    @Override
    public AjaxResult updateGroup(Group group)
    {
        group.setUpdateTime(DateUtils.getNowDate());
        Group sosSchool = groupMapper.schoolNameBySosGroup(group.getGroupName());
        if (AssertUtil.isNotEmpty(sosSchool)) {
            return AjaxResult.warn("学校名称已存在！");
        }
        groupMapper.updateGroup(group);
        return AjaxResult.success("添加成功！");
    }

    /**
     * 批量删除集团信息
     * 
     * @param ids 需要删除的集团信息主键
     * @return 结果
     */
    @Override
    public int deleteGroupByIds(String ids)
    {
        return groupMapper.deleteGroupByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除集团信息信息
     * 
     * @param id 集团信息主键
     * @return 结果
     */
    @Override
    public int deleteGroupById(String id)
    {
        return groupMapper.deleteGroupById(id);
    }

    @Override
    public AjaxResult add(Group group) {
        Group sosSchool = groupMapper.schoolNameBySosGroup(group.getGroupName());
        if (AssertUtil.isNotEmpty(sosSchool)) {
            return AjaxResult.warn("学校名称已存在！");
        }
        group.setId(UuidUtil.getUuid());
        groupMapper.insertGroup(group);
        return AjaxResult.success("添加成功！");
    }

    @Override
    public int insertAuthUsers(String groupId, String[] staffIds) {
        for (int i = 0; i < staffIds.length ; i++) {
            GroupUser groupUser = groupUserMapper.selectGroupUserByGroupId(groupId,staffIds[i]);
            if (AssertUtil.isEmpty(groupUser)){
                GroupUser groupUser1 = new GroupUser();
                groupUser1.setGroupId(groupId);
                groupUser1.setUserId(staffIds[i]);
                groupUserMapper.insertGroupUser(groupUser1);
            }
        }
        return 1;
    }

    @Override
    public int cancelAll(String groupId, String userIds) {
        return groupUserMapper.deleteGroupUserByGroupIds(groupId, Convert.toStrArray(userIds));
    }


    @Override
    public List<Group> selectStaffIdByGroupList(String staffId) {
        return groupUserMapper.selectStaffIdByGroupList(staffId);
    }
}
